package com.sctvcloud.bazhong.ui.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

import com.sctvcloud.bazhong.beans.FocusColumn;
import com.sctvcloud.bazhong.ui.fragment.ApplyFragment;
import com.sctvcloud.bazhong.ui.fragment.ColumnBaseFragment;
import com.sctvcloud.bazhong.ui.fragment.ReportFragment;
import com.sctvcloud.bazhong.ui.fragment.ShareFragment;
import com.sctvcloud.bazhong.ui.fragment.TvFragment;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by Administrator on 2017/6/19 0019.
 * 首页 管理各个栏目的页面适配器
 */

public class PlateFormPagerAdapter extends FragmentStatePagerAdapter {

    public static final int TYPE_APPLY = 0;//民生诉求
    public static final int TYPE_REWARD = 1;//举报有奖
    public static final int TYPE_SHARE = 2;//互动精选
    public static final int TYPE_REPORT = 3;//举报
    public static final int TYPE_HUDONG = 4;//互动分享
    private List<FocusColumn> list;
    private TvFragment tv;
    private FocusColumn specail;
    private FocusColumn anchor;
    private SparseArray<WeakReference<ColumnBaseFragment>> fragmentWeaks;

    private int topHeight;
    private boolean is_mine;

    public boolean isIs_mine() {
        return is_mine;
    }

    public void setIs_mine(boolean is_mine) {
        this.is_mine = is_mine;
    }

    public PlateFormPagerAdapter(FragmentManager fm, List<FocusColumn> list) {
        super(fm);
        this.list = list;
        fragmentWeaks = new SparseArray<>();
    }

    @Override
    public Fragment getItem(int position) {
        ColumnBaseFragment fragment = null;
        switch (list.get(position).getChannelType()) {
            case "" + TYPE_APPLY:
                Bundle bundle4 = new Bundle();
                if(is_mine){
                    bundle4.putBoolean("is_mine",is_mine);
                }
                fragment = new ApplyFragment();
                fragment.setArguments(bundle4);
                break;
            case "" + TYPE_REWARD:
                Bundle bundle2 = new Bundle();
                bundle2.putString("reward", "reward");
                if(is_mine){
                    bundle2.putBoolean("is_mine",is_mine);
                }
                fragment = new ShareFragment();
                fragment.setArguments(bundle2);
                break;
            case "" + TYPE_SHARE:
                Bundle bundle1 = new Bundle();
                if(is_mine){
                    bundle1.putBoolean("is_mine",is_mine);
                }
                fragment = new ShareFragment();
                fragment.setArguments(bundle1);
                break;
            case "" + TYPE_REPORT:
                Bundle bundle = new Bundle();
                bundle.putString("my", "my");
                if(is_mine){
                    bundle.putBoolean("is_mine",is_mine);
                }
                fragment = new ApplyFragment();
                fragment.setArguments(bundle);
                break;
            case "" + TYPE_HUDONG:
                Bundle bundle3 = new Bundle();
                bundle3.putString("hudong", "hudong");
                if(is_mine){
                    bundle3.putBoolean("is_mine",is_mine);
                }
                fragment = new ShareFragment();
                fragment.setArguments(bundle3);
                break;
            default:
                fragment = new ApplyFragment();
                break;
        }



        fragment.setChannelUrl(list.get(position).getChannelUrl());
        fragment.setIndexInViewPager(position);
        fragmentWeaks.put(position, new WeakReference<ColumnBaseFragment>(fragment));
        return fragment;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getChannelName();
    }

    public void setData(List<FocusColumn> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    public ColumnBaseFragment getFragmentByPos(int pos) {
        WeakReference<ColumnBaseFragment> wrf = fragmentWeaks.get(pos);
        if (wrf != null) {
            return wrf.get();
        }
        return null;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
    }

    public int getPosCurrentPage(String type) {
        if (getCount() == 0) {
            return -1;
        }
        for (int i = 0; i < getCount(); i++) {
            if (list.get(i).getChannelType().equals(type)) {
                return i;
            }
        }
        return -1;
    }

    public void setTopHeight(int topHeight) {
        this.topHeight = topHeight;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }


}
